from django.db import models
import livesociety.settings as settings
import os
from random import getrandbits


class User(models.Model):
    id = models.AutoField(primary_key=True)
    avatar = models.CharField(max_length=200, null=True)
    mail = models.EmailField(max_length=100, unique=True)
    name = models.CharField(max_length=25)
    lastname = models.CharField(max_length=25)
    gender = models.CharField(max_length=1, null=True)
    birthday = models.DateField(null=True) # ????
    bio = models.CharField(max_length=2000, null=True)
    password = models.CharField(max_length=32)
    friends = models.ManyToManyField('self', symmetrical=True)
    requests = models.ManyToManyField('self', symmetrical=False)
    def UpdateAvatar(self, avatar):
        dir = settings.DMS_ROOT + '/users/' + self.mail + '/avatar/'
        av_name = 'avatar_' + getrandbits(15).__str__() + '.jpg'
        if not os.path.exists(dir):
            os.makedirs(dir)
        destination = open(dir + av_name, 'wb+')
        for chunk in avatar.chunks():
            destination.write(chunk)
        destination.close()
        self.avatar = 'media/users/' + self.mail + '/avatar/' + av_name
        self.save()
    
    def __unicode__(self):
        return self.name

class Session(models.Model):
    id = models.AutoField(primary_key=True)
    user = models.ForeignKey(User, related_name='sessions')
    key = models.CharField(max_length=64)
    client_ip = models.IPAddressField();
    date = models.DateField(auto_now=True)
    time = models.TimeField(auto_now=True)
    

class location(models.Model):
    user = models.ForeignKey(User, related_name='location', primary_key=True) #????
    country = models.CharField(max_length=30)
    state = models.CharField(max_length=30)
    city = models.CharField(max_length=30)
    
    

class Status(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, related_name='states')
    content = models.CharField(max_length=200)
    date = models.DateField(auto_now=True)
    time = models.TimeField(auto_now=True)
    def __unicode__(self):
        return self.content
    

class PrivateMsg(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, related_name='privates_out')
    recipient = models.ForeignKey(User, related_name='privates_in')
    date = models.DateField(auto_now=True)
    time = models.TimeField(auto_now=True)
    content = models.CharField(max_length=1500)
    readed = models.BooleanField()


class Post(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, related_name='posts_out')
    date = models.DateField(auto_now=True)
    time = models.TimeField(auto_now=True)
    content = models.CharField(max_length=1500)
    to = models.ForeignKey(User, related_name='posts_in')


class Comment(models.Model):
    id = models.AutoField(primary_key=True)
    author = models.ForeignKey(User, related_name='comments')
    date = models.DateField(auto_now=True)
    time = models.TimeField(auto_now=True)
    content = models.CharField(max_length=1500)
    status = models.ForeignKey(Status, related_name='replies', null=True)
    post = models.ForeignKey(Post, related_name='replies', null=True)
    def __unicode__(self):
        return self.content